﻿Imports DotNetNuke.Entities.Modules.Definitions
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Tabs
Imports DotNetNuke.Services.Log.EventLog
Imports DotNetNuke.Services.Upgrade

Namespace dnnWerk.Providers.RadEditorProvider

    Public Class UpgradeController
        Implements IUpgradeable

        Public Function UpgradeModule(ByVal Version As String) As String Implements IUpgradeable.UpgradeModule
            Try
                If Version = "06.00.00" Then
                    'Create Rad Editor Config Page (or get existing one)
                    Dim newPage As TabInfo = Upgrade.AddHostPage("Editor Provider Configuration", "Provides configuration options for HTML Editor Provider.", "~/DesktopModules/dnnWerk.RadEditorProvider/images/radeditor_config_small.png", "~/DesktopModules/dnnWerk.RadEditorProvider/images/radeditor_config_large.png", True)

                    'Add Module To Page
                    Dim moduleDefId As Integer = GetModuleDefinitionID()
                    Upgrade.AddModuleToPage(newPage, moduleDefId, "Editor Provider Configuration", "~/DesktopModules/dnnWerk.RadEditorProvider/images/radeditor_config_large.png", True)
                End If
            Catch ex As Exception
                Dim xlc As New ExceptionLogController
                xlc.AddLog(ex)

                Return "Failed"
            End Try

            Return "Success"
        End Function

        Private Function GetModuleDefinitionID() As Integer
            ' get desktop module
            Dim desktopModule As DesktopModuleInfo = DesktopModuleController.GetDesktopModuleByModuleName("dnnWerk.RadEditorProvider", Null.NullInteger)
            If desktopModule Is Nothing Then
                Return -1
            End If

            'get module definition
            Dim moduleDefinition As ModuleDefinitionInfo = ModuleDefinitionController.GetModuleDefinitionByFriendlyName("dnnWerk.RadEditorProvider", desktopModule.DesktopModuleID)
            If moduleDefinition Is Nothing Then
                Return -1
            End If

            Return moduleDefinition.ModuleDefID
        End Function

    End Class

End Namespace
